<?php

namespace App\Http\Services;

use App\Http\Model\Gift;
use App\Http\Model\GroupChat;
use App\Lib\RcGroupChat;

class GroupChatMsgServices extends CommonServices
{
    private $rcGroupChat;
    private $groupChatMode;

    protected $groupPrefix = 'group_';// 群组前缀

    public function __construct()
    {
        parent::__construct();

        $this->rcGroupChat = RcGroupChat::getInstance();
        $this->groupChatMode = new GroupChat();
    }

    ############################################################################################################

    /**
     * 群组-发送消息-禁言
     *
     * @param $fromUserId int 发送人用户Id
     * @param $groupChatId int 群组Id
     * @return bool
     * */
    public function groupSendByForbidWords($fromUserId, $groupChatId)
    {
        $data = [
            'k' => 110003,
            'created_at' => time(),
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:forbid_words', $data, 0);
    }

    /**
     * 群组-发送消息-取消禁言
     *
     * @param $fromUserId int 发送人用户Id
     * @param $groupChatId int 群组Id
     * @return bool
     * */
    public function groupSendByCancelForbidWords($fromUserId, $groupChatId)
    {
        $data = [
            'k' => 110004,
            'created_at' => time(),
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:cancel_forbid_words', $data, 0);
    }

    /**
     * 群组-发送消息-成员加入群组
     *
     * @param $fromUserId int 发送人用户Id（管理员用户）
     * @param $groupChatId int 群组Id
     * @param $userId int 普通用户Id（加入群组的用户）
     * @return bool
     * */
    public function groupSendByJoinGroupSuccess($fromUserId, $groupChatId, $userId)
    {
        $user = $this->user->get_user($userId);

        $data = [
            'k' => 110005,
            'user_id' => $userId,
            'nickname' => $user['nickname'],
            'created_at' => time(),
            'rand_text' => [
                "欢迎小可爱加入我们这个温暖的大家庭",
                "少年，等你好久了",
                "少年，你来对地方啦",
                "新人要抓牢，我们带你飞"
            ]
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:join_group_success', $data, 0);
    }

    ############################################################################################################

    /**
     * 群组创建，每个群最多 3000 人，应用内群组数量无限制
     *
     * @param $groupChatId int 群组Id
     * @param $groupChatName string 群组名称，最大长度 60 个字符
     * @param $userId int 用户ID
     * @return bool
     * */
    public function groupCreate($groupChatId, $groupChatName, $userId)
    {
        $member = [
            ['id' => $userId],
        ];

        return $this->rcGroupChat->groupCreate($this->groupPrefix . $groupChatId, $groupChatName, $member);
    }

    /**
     * 获取群信息
     *
     * @param $groupChatId int 群组ID
     * @return array
     * */
    public function groupGet($groupChatId)
    {
        return $this->rcGroupChat->groupGet($this->groupPrefix . $groupChatId);
    }

    /**
     * 修改群信息
     *
     * @param $groupChatId int 群组ID
     * @param $groupChatName string 群组名称，最大长度 60 个字符
     * @return bool
     * */
    public function groupUpdate($groupChatId, $groupChatName)
    {
        return $this->rcGroupChat->groupUpdate($this->groupPrefix . $groupChatId, $groupChatName);
    }

    /**
     * 加入群组
     *
     * @param $groupChatId int 群组ID
     * @param $groupChatName string 群组名称，最大长度 60 个字符
     * @param $userId int 用户ID
     * @return bool
     * */
    public function groupJoin($groupChatId, $groupChatName, $userId)
    {
        $member = ['id' => $userId];

        return $this->rcGroupChat->groupJoin($this->groupPrefix . $groupChatId, $groupChatName, $member);
    }

    /**
     * 退出群组
     *
     * @param $groupChatId int 群组ID
     * @param $userId int 用户ID
     * @return bool
     * */
    public function groupQuit($groupChatId, $userId)
    {
        $member = ['id' => $userId];

        return $this->rcGroupChat->groupQuit($this->groupPrefix . $groupChatId, $member);
    }

    /**
     * 解散群组
     *
     * @param $groupChatId int 群组ID
     * @param $userId int 用户ID
     * @return bool
     * */
    public function groupDismiss($groupChatId, $userId)
    {
        $member = ['id' => $userId];

        return $this->rcGroupChat->groupDismiss($this->groupPrefix . $groupChatId, $member);
    }

    ############################################################################################################

    /**
     * 群组Id参数
     *
     * @param $value mixed (int/string/array) 数据值 本地支持 groupId 数字或数组或字符串拼接格式: 1 / [1,2,3] / 1,2,3
     * @return array
     * */
    protected function requestGroupIdParams($value)
    {
        $params = [];

        if (empty($value)) {
            return $params;
        }

        if (is_numeric($value)) {
            $value = [$value];
        } elseif (is_string($value)) {
            if (strpos($value, ',') !== false) {
                $value = array_filter(explode(',', $value));
            } else {
                $value = [$value];
            }
        }

        foreach ($value as $val) {
            $params[] = $this->groupPrefix . $val;
        }

        return $params;
    }

    ############################################################################################################

    /**
     * 群组-整体禁言-添加
     *
     * @param $groupChatId mixed (int/string/array) 群组 Id，支持一次设置多个，最多不超过 20 个
     *
     * @return bool
     * */
    public function groupBanAdd($groupChatId)
    {
        $groupChatId = $this->requestGroupIdParams($groupChatId);
        return $this->rcGroupChat->groupBanAdd($groupChatId);
    }

    /**
     * 群组-整体禁言-移除
     *
     * @param $groupChatId mixed (int/string/array) 群组 Id，支持一次设置多个，最多不超过 20 个
     * @return bool
     * */
    public function groupBanRollback($groupChatId)
    {
        $groupChatId = $this->requestGroupIdParams($groupChatId);
        return $this->rcGroupChat->groupBanRollback($groupChatId);
    }

    /**
     * 群组-整体禁言-获取
     *
     * @param $groupChatId mixed (int/string/array) 群组 Id，不传此参数，表示查询所有设置禁言的群组列表；传此参数时，表示查询传入的群组 Id 是否被设置为群组禁言，支持一次查询多个，最多不超过 20 个
     * @return array
     * */
    public function groupBanQuery($groupChatId = '')
    {
        $groupChatId = $this->requestGroupIdParams($groupChatId);
        return $this->rcGroupChat->groupBanQuery($groupChatId);
    }

    ############################################################################################################


    /**
     * 群组-整体禁言-白名单-添加
     *
     * @param $userId mixed (int/string/array) 用户 Id，支持一次添加多个用户，最多不超过 20 个
     * @param $groupChatId mixed (int/string/array) 群组 Id
     *
     * @return bool
     * */
    public function groupUserBanWhiteAdd($userId, $groupChatId)
    {
        $groupChatId = $this->requestGroupIdParams($groupChatId);
        return $this->rcGroupChat->groupUserBanWhiteAdd($userId, $groupChatId);
    }

    /**
     * 群组-整体禁言-白名单-移除
     *
     * @param $userId mixed (int/string/array) 用户 Id，支持一次添加多个用户，最多不超过 20 个
     * @param $groupChatId mixed (int/string/array) 群组 Id
     * @return bool
     * */
    public function groupUserBanWhiteRollback($userId, $groupChatId)
    {
        $groupChatId = $this->requestGroupIdParams($groupChatId);
        return $this->rcGroupChat->groupUserBanWhiteRollback($userId, $groupChatId);
    }

    /**
     * 群组-整体禁言-白名单-获取
     *
     * @param $groupChatId mixed (int/string/array) 群组 Id
     * @return array
     * */
    public function groupUserBanWhiteQuery($groupChatId)
    {
        $groupChatId = $this->requestGroupIdParams($groupChatId);
        return $this->rcGroupChat->groupUserBanWhiteQuery($groupChatId);
    }

    /**
     * 群组-发送消息-群管理职位变更消息
     *
     * @param $userId int 职位变更用户ID
     * @param $groupChatId int 群组Id
     * @param $memberType 0=普通成员，1=管理者
     * @return bool
     * */
    public function groupSendByPostionUpdateMsg($userId, $groupChatId, $memberType = 0)
    {
        $user = $this->user->get_user($userId);

        $data = [
            'k' => 110006,
            'user_id' => $userId,
            'nickname' => $user['nickname'],
            'msg' => "被任命为群聊管理",
            'created_at' => time(),
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:group_position_success', $data, 0);
    }

    /**
     * 群组-发送消息-群公会职位变更消息
     *
     * @param $userId int 职位变更用户ID
     * @param $groupChatId int 群组Id
     * @param $memberType int 公会职位类型
     * @param $msg str 消息内容
     * @return bool
     * */
    public function groupSendByUnionPostionUpdateMsg($userId, $groupChatId, $memberType = 0, $msg = '职位变更')
    {
        $user = $this->user->get_user($userId);

        $data = [
            'k' => 110007,
            'user_id' => $userId,
            'nickname' => $user['nickname'],
            'msg' => $msg,
            'created_at' => time(),
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:group_union_position_success', $data, 0);
    }

    /**
     * 群组-发送消息-群信息变更消息
     *
     * @param $userId int 变更用户ID
     * @param $groupChatId int 群组Id
     * @param $msg string 文案
     * @return bool
     * */
    public function groupSendByUpdateGroupChatMsg($userId, $groupChatId, $msg = "群信息")
    {
        $user = $this->user->get_user($userId);

        $data = [
            'k' => 110008,
            'user_id' => $userId,
            'nickname' => $user['nickname'],
            'msg' => $msg,
            'created_at' => time(),
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:group_update_success', $data, 0);
    }

    /**
     * 群组-发送消息-发送用户所在群 用户等级变更群消息
     *
     * @param $userId int 变更用户ID
     * @param $msg string 显示文案
     * @return bool
     * */
    public function groupSendByUserRankMsg($userId, $msg = "等级变更")
    {
        $user = $this->user->get_user($userId);

        $data = [
            'k' => 110009,
            'user_id' => $userId,
            'nickname' => $user['nickname'],
            'msg' => $msg,
            'created_at' => time(),
            'rand_text' => [
               '可以的老铁！666',
               '厉害了厉害了',
               '老哥，稳',
               '明明可以靠脸吃饭，偏偏要靠才华'
            ]
        ];

        //获取用户所在群组
        $groupChatIds = $this->groupChatMode->getGroupChatIdsByUserId($userId);
        if ($groupChatIds) {
            foreach ($groupChatIds as $key => $value) {
                $groupChatId = $value;
                $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:group_user_rank_success', $data, 0);
            }
        }

        return true;
    }

    public function unionDepositDraw($union_id, $msg = "【会长已申请提取公会资质认证金】")
    {
        $data = [
            'k' => 110010,
            'msg' => $msg,
            'created_at' => time(),
        ];

        //获取用户所在群组
        $groupChatId = $this->groupChatMode->getGroupChatByUnion($union_id);
        $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:group_union_deposit_draw', $data, 0);
        return true;
    }

    /**家族职位变更消息+解散群消息
     * @param $userId
     * @param $groupChatId
     * @param string $role_name
     * @param $type 1职位变更 2解散
     * @return bool
     */
    public function groupSendByFamilyPostionUpdateMsg($userId, $groupChatId, $role_name = '职位变更' ,$type = 1)
    {
        $user = $this->user->get_user($userId);

        $data = [
            'k' => 110011,
            'user_id' => $userId,
            'nickname' => $user['nickname'],
            'msg' => $user['nickname'].'职位变更为'.$role_name,
            'type' => $type,
            'created_at' => time(),
        ];

        return $this->rcGroupChat->groupSend(9, $this->groupPrefix . $groupChatId, 'kk:group_family_position_success', $data, 0);
    }

    /**公会解散消息
     * @param $userId
     * @param $groupChatId
     * @param $msg
     * @return bool
     */
    public function groupSendUnionDissolveMsg($userId, $groupChatId ,$msg)
    {
        $data = [
            'k' => 110012,
            'user_id' => $userId,
            'msg' => $msg,
            'created_at' => time(),
        ];
        return $this->rcGroupChat->groupSend(10, $this->groupPrefix . $groupChatId, 'kk:group_union_dissolve', $data, 0);
    }

    /**群聊里送礼物消息
     * @param $send_user_id
     * @param $recv_user_id
     * @param $gift_id
     * @param $gift_count
     * @param $groupChatId
     * @param $type 1 钻石 2金豆
     * @return bool|void
     */
    public function groupSendGiftGiveMsg($send_user_id, $recv_user_id, $gift_id, $gift_count,$groupChatId ,$type=1)
    {
        if($type == 1){
            $model_gift = new Gift();
            $gift = $model_gift->get_gift($gift_id);
        }else{
            return;
        }
        $send_user = $this->user->get_user($send_user_id);
        $recv_user = $this->user->get_user($recv_user_id);

        if (empty($send_user) || empty($recv_user) || empty($gift)) {
            return;
        }
        $suser_image = '';
        if (!empty($send_user['images'])) {
            $suser_image = $send_user['images'][0];
        }
        $ruser_image = '';
        if (!empty($recv_user['images'])) {
            $ruser_image = $recv_user['images'][0];
        }
        $data = [
            "k" => 110013,
            "gid" => $gift['gift_id'],
            "gc" => $gift_count,
            "gn" => $gift['name'],
            "img" => $gift['image'],
            "res" => $gift['animate_resource'],  // 礼物资源
            "suid" => $send_user['user_id'],        // 发送人用户ID
            "snn" => $send_user['nickname'],      // 发送人用户昵称
            "simg" => $suser_image,
            "ruid" => $recv_user['user_id'],       // 接受人用户ID
            "rnn" => $recv_user['nickname'],     // 接受人用户昵称
            "rimg" => $ruser_image,
            "type" =>$type
        ];
        return $this->rcGroupChat->groupSend(11, $groupChatId, 'kk:group_send_gift', $data, 0);
    }

    /**创建群组消息
     * @param $groupChatId
     * @param $msg
     * @return bool
     */
    public function groupCreateGroupSuccess($groupChatId, $msg)
    {

        $data = [
            'k' => 110014,
            'msg'=> $msg ,
            'create_time' => time()
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:create_group_success', $data, 0);
    }

    /**取消任命群管理
     * @param $userId
     * @param $groupChatId
     * @return bool
     */
    public function groupSendPositionCancelMsg($userId, $groupChatId)
    {
        $data = [
            'k' => 110015,
            'user_id' => $userId,
            'created_at' => time(),
        ];

        return $this->rcGroupChat->groupSend(11, $this->groupPrefix . $groupChatId, 'kk:group_position_cancel', $data, 0);
    }
}